package com.yonyou.crm.sys.modules.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.sys.modules.entity.BlockItemVO;
import com.yonyou.crm.sys.modules.entity.FieldVO;
import com.yonyou.crm.sys.modules.entity.ModuleVO;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class BlockItemBizServiceImpl extends BaseBizServiceImpl<BlockItemVO>{

	@Autowired
	private FieldBizServiceImpl fieldBizServiceImpl;
	/**
	 * 获取分组明细列表
	 * 
	 * @return
	 */
	public List<BlockItemVO> getBlockItemList(Long blockId){

		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("blockId", blockId);

		List<BlockItemVO> blockItemList = new ArrayList<BlockItemVO>();
		blockItemList = dao.selectVoList(BlockItemVO.class, "ListBlockItem", paramMap);
		for (BlockItemVO blockItemVO : blockItemList) {
			if (blockItemVO.getIsBlank() == 1) {
				continue;
			}
			FieldVO field = fieldBizServiceImpl.getField(blockItemVO.getActualFieldFullname());
			blockItemVO.setActualField(field);
		}		
		return blockItemList;
	}
	
	public BlockItemVO addBlockItem(ModuleVO moduleVO,BlockItemVO blockItemVO) {
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		String userIdStr = (String)InvocationInfoProxy.getParameter("id");//用户id
		Long userId = Long.parseLong(userIdStr);
		blockItemVO.setTenantId(tenantId);
		Date now = new Date();
		blockItemVO.setSysCreatedTime(now);
		blockItemVO.setSysModifiedTime(now);
		blockItemVO.setSysCreatedUserId(userId);
		blockItemVO.setSysModifiedUserId(userId);
		if (blockItemVO.getIsBlank() == 0) {
			FieldVO fieldVO = fieldBizServiceImpl.getField(moduleVO.getId(), blockItemVO.getApiName());
			blockItemVO.setActualFieldFullname(fieldVO.getFullname());
		}
		blockItemVO = super.saveVO(blockItemVO);
		return blockItemVO;
	}
	
	public void deleteItems (List<Long> blockIds)  {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String tenantIdStr = (String) InvocationInfoProxy.getTenantid();
		Long tenantId = Long.parseLong(tenantIdStr);
		paramMap.put("tenantId", tenantId);
		paramMap.put("blockIds", blockIds);
		dao.deleteSql(BlockItemVO.class, "deleteAllItems", paramMap);	
	}
	
}



